<template>
	<view class="phraseText">
		<view class="info">
			<view class="textarea">
				<textarea style="width: 100%; height:100%;" :inputBorder="false" maxlength="200" v-model="content"
					type="textarea" placeholder="请输入内容" row="20"></textarea>
				<view class="number">
					<text class="text">{{content.length ? content.length : 0}}</text>/200
				</view>
			</view>
		</view>
		<view class="btn" @click="handleSave">完成</view>
	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	// import { addPhrase, editPhrase } from "@/utils/api.js"
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			return {
				title: '编辑常用语',
				type: 'add',
				content: '',
				id: ''
			};
		},
		onLoad(o) {
			this.type = o.type
			if (o && o.content) {
				this.content = decodeURIComponent(o.content) ? decodeURIComponent(o.content) : ''
			}
			this.id = o.id
			if (this.type == 'edit') {
				this.title = '编辑常用语'
			} else if (this.type == 'add') {
				this.title = '添加常用语'
			}
			uni.setNavigationBarTitle({
				title: this.title
			})
		},
		computed: {
			...mapState(['imToken'])
		},
		methods: {
			// 保存
			handleSave() {
				if (this.type == 'edit') {
					this.editPhrase()
				} else if (this.type == 'add') {
					this.addPhrase()
				}
			},
			// 编辑常用语
			editPhrase() {
				http.post(api.phraseEdit, {
					token: this.imToken,
					content: this.content,
					id: this.id
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: res.data.message,
							icon: "success"
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				})
			},
			//添加常用语
			addPhrase() {
				http.post(api.phraseAdd, {
					token: this.imToken,
					content: this.content
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: res.data.message,
							icon: "success"
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.phraseText {
		width: 100%;
		height: 100%;

		.title {
			height: 80rpx;
			line-height: 80rpx;
			color: #333;
			font-weight: bold;
			font-size: 30rpx;
			text-align: center;
		}

		.preservation {
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			// color: $textActiveColor;
		}

		.info {
			height: calc(100% - 138rpx - 12rpx);

			.textarea {
				height: 100%;
				border-bottom: 1rpx solid #ededed;
				padding-bottom: 10rpx;
				position: relative;
			}

			::v-deep .uni-easyinput__content-textarea {
				min-height: 432rpx;
				height: 1350rpx;
				padding: 0 36rpx;
				margin: 0;
			}

			::v-deep .uni-textarea-placeholder {
				padding-top: 30rpx;
				box-sizing: border-box;
			}

			::v-deep .uni-textarea-textarea {
				padding-top: 30rpx;
				box-sizing: border-box;
			}

			.number {
				border: 1rpx solid #ededed;
				text-align: right;
				color: #C6C6C6;
				font-size: 28rpx;
				padding: 30rpx 36rpx;
				position: absolute;
				left: 0;
				right: 0;
				bottom: -4rpx;

				.text {
					// color: $textActiveColor;
					font-size: 28rpx;
				}
			}
		}

		.btn {
			background: #1787fb;
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 80rpx
		}
	}
</style>